<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of PostModel
 * 文章关联模型
 * @author yihua
 */

namespace Common\Model;

use Think\Model;

class TagModel extends Model {

    protected $tableName = 'tag';
    //验证规则项
    protected $_validate = array(
        array('alias', 'require', '请输入别名', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
        array('tag_name', 'require', '请输入标签名', self::EXISTS_VALIDATE, 'regex', self::MODEL_BOTH),
        array('alias', 'checkTagAlias', '标签别名的格式不正确', self::EXISTS_VALIDATE, 'callback', self::MODEL_BOTH),
        array('alias', 'isTagAliasNew', '标签别名已被使用', self::EXISTS_VALIDATE, 'callback', self::MODEL_BOTH), // 自定义函数验证密码格式
        array('tag_name', 'isTagNameNew', '标签名已被使用', self::EXISTS_VALIDATE, 'callback', self::MODEL_BOTH),
    );
    //自动添加项
    protected $_auto = array(
        array('create_time', NOW_TIME, self::MODEL_INSERT),
        array('modify_time', NOW_TIME, self::MODEL_BOTH)
    );

    /**
     * 加载标签列表
     */
    public function loadList($map) {
        $options = $this->options;
        $this->options = array();
        $count = $this->where($map)->count();
        $list['count'] = $count;
        $this->options = $options;
        $list['page'] = loadPage($count, $this->options['limit']);
        $list['list'] = $this->where($map)->select();
        return $list;
    }

    /**
     * 加载热门标签
     */
    public function loadHot($limit) {
        $this->limit($limit)->order('frequency desc');
        $list = $this->loadList();
        return $list;
    }

    /**
     * 插入标签
     * @param type $data
     * @return boolean
     */
    public function insert($data) {
        $tag = $this->create($data);
        if (empty($tag)) {
            return false;
        }
        if ($res = $this->add($tag)) {
            return $res;
        } else {
            $this->error = "插入失败！";
            return false;
        }
    }

    /**
     * 更新标签
     * @param type $data
     * @return boolean
     */
    public function modify($data) {
        $tag = $this->create($data);
        if (empty($tag)) {
            return false;
        }
        $res = $this->where(array('tag_id' => $tag['tag_id']))->save($tag);
        if ($res === false) {
            $this->error = "保存失败！";
            return false;
        } else {
            return true;
        }
    }

    public function setOpt($options) {
        $this->setProperty('options', $options);
        return $this;
    }

    /**
     * 检测标签别名的格式
     * @param type $alias
     * @return boolean
     */
    public function checkTagAlias($alias) {
        $pattern = '/^(\w)+$/';
        if (preg_match($pattern, $alias)) {
            return true;
        } else {
            return false;
        }
    }

    public function isTagAliasNew($alias) {
        return !$this->isTagAliasUsed($alias);
    }

    public function isTagNameNew($name) {
        return !$this->isTagNameUsed($name);
    }

    /**
     * 检测标签别名的格式
     * @param type $alias
     * @return boolean
     */
    public function isTagAliasUsed($alias, $id = 0) {
        $map['alias'] = $alias;
        if (!empty($id)) {
            $map['tag_id'] = array('neq', $id);
        } else {
            $map['tag_id'] = array('neq', I('tag_id'));
        }
        if ($this->where($map)->find()) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 检测标签别名的格式
     * @param type $alias
     * @return boolean
     */
    public function isTagNameUsed($name, $id = 0) {
        $map['tag_name'] = $name;
        if (!empty($id)) {
            $map['tag_id'] = array('neq', $id);
        } else {
            $map['tag_id'] = array('neq', I('tag_id'));
        }
        if ($this->where($map)->find()) {
            return true;
        } else {
            return false;
        }
    }

}
